package cn.itsuorce.controller;

import cn.itsuorce.domain.dto.ShopDto;
import cn.itsuorce.domain.dto.UserDto;
import cn.itsuorce.service.IShopService;
import cn.itsuorce.service.IUserService;
import cn.itsuorce.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录和注册控制器
 */
@RestController
@RequestMapping("/lrc")
public class LoginAndRegisterController {

    @Autowired
    private IShopService shopService;
    @Autowired
    private IUserService userService;

    /**
     * 商家入驻
     * @param shopDto
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody ShopDto shopDto){
        try {
            return shopService.settledIn(shopDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("商家入驻失败，我们正在使劲殴打程序员..." + e.getMessage());
        }
    }


    /**
     * 验证手机号
     * @param type 1表示手机验证码注册 2表示验证码登录
     * @param phone
     * @return
     */
    @GetMapping("/validatePhone/{type}/{phone}")
    public AjaxResult validatePhone(@PathVariable("type")Integer type,@PathVariable("phone")String phone){
        return userService.validatePhone(type, phone);
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendMobileCode/{phone}")
    public AjaxResult sendMobileCode(@PathVariable("phone")String phone){
        return userService.sendMobileCode(phone);
    }

    /**
     * 短信验证码注册:提交表单
     * @param userDto
     * @return
     */
    @PostMapping("/phoneReg")
    public AjaxResult phoneReg(@RequestBody UserDto userDto){
        return userService.phoneReg(userDto);
    }

    /**
     * 用户登录
     * type 1表示前台  2表示后台
     * @param userDto
     * @return
     */
    @PostMapping("/accountLogin")
    private AjaxResult accountLogin(@RequestBody UserDto userDto){
        System.out.println(userDto);
        return userService.accountLogin(userDto);
    }
    /**
     * 微信扫码登录
     * @param code
     * @return
     */
    @GetMapping("/wechatLogin/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        return userService.wechatLogin(code);
    }

    /**
     * 微信用户扫码登录后，绑定我们系统的用户（等同于注册）
     * @param userDto 实体类中要添加两个字段：access_token和openid
     * @return
     */
    @PostMapping("/wechatUserBind")
    public AjaxResult wechatUserBind(@RequestBody UserDto userDto){
        return userService.wechatUserBind(userDto);
    }
}